package com.example.app2024.controls;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app2024.R;

import java.util.ArrayList;
import java.util.List;


public class ApplistActivity extends AppCompatActivity
{

      ListView listView;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_applist);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            listView = findViewById(R.id.applist);
            List<String> appnames = new ArrayList<>();
            appnames.add("qq");
            appnames.add("qq1");
            appnames.add("qq22");

            listView.setAdapter(new AppListAdapter(getAppInfos()));
            final List<ResolveInfo> appInfos = getAppInfos();

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                  {
                        String packageName = appInfos.get(position).activityInfo.packageName;
                        String className = appInfos.get(position).activityInfo.name;
                        ComponentName componentName = new ComponentName(packageName, className);
                        final Intent intent = new Intent();
                        intent.setComponent(componentName);
                        startActivity(intent);
                  }
            });

            LayoutInflater inflater = this.getLayoutInflater();
            View headView = inflater.inflate(R.layout.header_list_view, null);
            listView.addHeaderView(headView);


      }

      /**
       * @return
       */
      private List<ResolveInfo> getAppInfos()
      {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            return getPackageManager().queryIntentActivities(intent, 0);
      }

      public class AppListAdapter extends BaseAdapter
      {
            List<ResolveInfo> mappInfo;

            public AppListAdapter(List<ResolveInfo> appInfos)
            {
                  mappInfo = appInfos;
            }

            @Override
            public int getCount()
            {
                  return mappInfo.size();
            }

            @Override
            public Object getItem(int position)
            {
                  return mappInfo.get(position);
            }

            @Override
            public long getItemId(int position)
            {
                  return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                  ViewHolder viewHolder = new ViewHolder();
                  if (convertView == null)
                  {
                        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = layoutInflater.inflate(R.layout.applistitem, null);
                        if (position % 2 == 0)
                        {

                              LinearLayout linearLayout = convertView.findViewById(R.id.ll2);
                              linearLayout.setBackgroundColor(Color.BLUE);

                        } else
                        {
                              LinearLayout linearLayout = convertView.findViewById(R.id.ll2);
                              linearLayout.setBackgroundColor(Color.WHITE);
                        }
                        viewHolder.mAppIconImageView = convertView.findViewById(R.id.icon_image_view);
                        viewHolder.mAppNameTextView = convertView.findViewById(R.id.appnametextview);
                        convertView.setTag(viewHolder);
                  } else
                  {
                        viewHolder = (ViewHolder) convertView.getTag();
                  }


                  viewHolder.mAppNameTextView.setText(mappInfo.get(position).activityInfo.loadLabel(getPackageManager()));
                  viewHolder.mAppIconImageView.setImageDrawable(mappInfo.get(position).activityInfo.loadIcon(getPackageManager()));

                  return convertView;
            }

            public class ViewHolder
            {
                  public ImageView mAppIconImageView;
                  public TextView mAppNameTextView;
            }

            //================================不同行不同效果

            //有几种不同的布局
            @Override
            public int getViewTypeCount()
            {
                  return 2;
            }

            @Override
            public int getItemViewType(int position)
            {
                  return position % 2;
            }
      }
}